package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void importOrderSettings(ArrayList<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());

            if (count > 0) {
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            } else {
                orderSettingMapper.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map> findOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";

        String dateEnd = date + "-31";

        List<OrderSetting> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin, dateEnd);

        List list = new ArrayList();

        for (OrderSetting orderSetting : orderSettings) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());

            list.add(map);
        }
        return list;
    }

    @Override
    public void updateNumberByOrderDate(OrderSetting orderSetting) {
        Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingMapper.updateNumberByOrderDate(orderSetting);
        } else {
            orderSettingMapper.add(orderSetting);
        } 
    }
}
